【问题标题】:How to add a css style class atribute to links generated from wp_get_archives?如何将 css 样式类属性添加到从 wp_get_archives 生成的链接?
【发布时间】:2016-02-23 12:34:54
【问题描述】:

对于我正在开发的一个 wordpress 自定义主题,

我必须做什么才能获得: <a class="archive_link" href...> 而不是:<a href...>

从 wp_get_archives 函数获取一个小部件?

在functions.php中我有这个:

function sxo_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Primary Widget Area', 'sxo' ),
        'id' => 'sidebar-1',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h1 class="widget-title">',
        'after_title' => '</h1>'
    ) );
}
add_action( 'widgets_init', 'sxo_widgets_init' );

在side.php 模板中我有:

...
<ul>
    <?php wp_get_archives( array( 'type' => 'monthly') ); ?>
</ul>
...

(已更新)而且,我忘了在 funtions.php 中说:

function sxo_widgets_init() {
    register_sidebar( array(
       'name' => __( 'Primary Widget Area', 'sxo' ),
       'id' => 'sidebar-1',
       'before_widget' => '<aside id="%1$s" class="widget %2$s">',
       'after_widget' => '</aside>',
       'before_title' => '<h1 class="widget-title">',
       'after_title' => '</h1>'
   ) );
}
add_action( 'widgets_init', 'sxo_widgets_init' );

(更新 2-aside.php:)

<?php
/**
* The Sidebar containing the main widget areas.
*
* @package sxo
* @since sxo 1.0
*/
?>
<div id="secondary" class="widget-area" role="complementary">
    <?php do_action( 'before_sidebar' ); ?>
    <?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>

        <aside id="search" class="widget widget_search">
            <?php get_search_form(); ?>
        </aside>

        <aside id="archives" class="widget">
            <h1 class="widget-title"><?php _e( 'Archives', 'sxo' ); ?></h1>
            <ul class="archive-sidebar">    
                 <?php wp_get_archives( array( 'type' => 'monthly') ); ?> <!--'before' => '<a class="widget-link">', 'after' => '</a>'?>-->
                  <?php /*wp_get_archives( array( 'type' => 'monthly', 'format' => 'custom', 'before' => '<a class="widget-link">', 'after' => '</a>') ); ?> <!--'before' => '<a class="widget-link">', 'after' => '</a>'?>-->


                 <?php //<?php wp_get_archives( 'format=custom&before=<p id="test">&after=</p>&type=postbypost&limit=100' ); 
                /*Step #3

                Now instead of a unordered list using <li></li>, the code will now be <p></p> and you are ready to go to town on customizations.  Endless possibilities.*/?>
            </ul>
        </aside>

        <aside id="meta" class="widget">
            <h1 class="widget-title"><?php _e( 'Meta', 'sxo' ); ?></h1>
            <ul>
                <?php wp_register(); ?>
                <li><?php wp_loginout(); ?></li>
                <?php wp_meta(); ?>
            </ul>
        </aside>

    <?php endif; // end sidebar widget area ?>
</div><!-- #secondary .widget-area -->

<?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>

     <div id="tertiary" class="widget-area" role="supplementary">
      <?php dynamic_sidebar( 'sidebar-2' ); ?>
     </div><!-- #tertiary .widget-area -->

<?php endif; ?>

(更新 3)- 哪里(从我的 front-page.php 模板中提取的代码)我正在尝试包含 aside.php:

<?php
/*
Template Name: Front-page - Portada de sxo
   @package WordPress
 * @subpackage sxo
*/

get_header('front-page'); ?>

<div id="contenido" class="wrapper clearfix">
<section id="list_posts_home">
            <div id="content" class="widecolumn">
                 <?php if (have_posts()) : while (have_posts()) : the_post();?>

                     <div class="listed_post_home">
                        <h2 id="post-<?php the_ID(); ?>"><a class="linkHomeBlog" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
                        <div class="entrytext">
                            <div style="float:left;margin-right:1.5em;"><?php the_post_thumbnail(); ?></div>
                            <p><?php
                            the_excerpt('<a href="<?php the_permalink(); ?>">Read more</a></p>');?>
                            <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
                        </div>
                     </div>
                <?php endwhile; 
                sxo_content_nav( 'nav-below' );
                endif; ?>
            </div>
<?php get_sidebar(); ?>

</section>

(更新 4)sidebar.php 模板:

<?php
/**
 * The sidebar containing the main widget area.
 *
 * If no active widgets in sidebar, let's hide it completely.
 *
 * @package WordPress
 * @subpackage sxo
 * @since sxo
 */
?>

    <?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
        <div id="secondary" class="widget-area" role="complementary">
            <?php dynamic_sidebar( 'sidebar-1' ); ?>
        </div><!-- #secondary -->
    <?php endif; ?>

提前致谢!

【问题讨论】:

    标签: php css wordpress class widget


    【解决方案1】:

    从我的其他答案中的 cmets 开始。将sidebar.php 更新为以下内容应该可以满足您的需求。这是原始问题中发布的边栏和侧边栏文件的组合,为清楚起见删除了 cmets。

    <?php
    /**
    * The Sidebar containing the main widget areas.
    *
    * @package sxo
    * @since sxo 1.0
    */
    ?>
    <div id="secondary" class="widget-area" role="complementary">
    
        <aside id="search" class="widget widget_search">
            <?php get_search_form(); ?>
        </aside>
    
        <aside id="archives" class="widget">
            <h1 class="widget-title"><?php _e( 'Archives', 'sxo' ); ?></h1>
            <ul class="archive-sidebar">   
                <?php wp_get_archives( array( 'type' => 'monthly') ); ?> 
            </ul>
        </aside>
    
        <aside id="meta" class="widget">
            <h1 class="widget-title"><?php _e( 'Meta', 'sxo' ); ?></h1>
            <ul>
                <?php wp_register(); ?>
                <li><?php wp_loginout(); ?></li>
                <?php wp_meta(); ?>
            </ul>
        </aside>
    
        <?php do_action( 'before_sidebar' ); ?>
    
        <?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
            <?php dynamic_sidebar( 'sidebar-1' ); ?>
        <?php endif; ?>
    
        <?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>
            <?php dynamic_sidebar( 'sidebar-2' ); ?>
        <?php endif; ?>
    </div>
    

    这首先显示您的“静态内容”,然后是 sidebar-1,然后是 sidebar-2。

    【讨论】:

    • @jgc412 - 用这个答案中的代码替换 sidebar.php 的内容并告诉我。确保您也在查看网站主页。
    • 我刚刚替换了这个,@tonyedwardspz,我重新加载了网络浏览器,生成的 html 仍然是一样的
    • 是的!非常感谢@tonyedwardspz!有用!我不知道为什么它以前没有工作。再次重命名文件名后,我在浏览器中重新加载了网页,最后它已经工作了。为什么会这样?我用的是火狐,还有灯
    • @jgc412 这就是 WordPress 的乐趣,但至少它有效。由于这解决了您的问题,您可以将其标记为已接受吗?
    【解决方案2】:

    您正在使用的注册侧边栏功能构建围绕小部件的 html,而不是小部件中的实际代码。但是,您的代码表明您直接在 php 文件中输出链接,而不是通过小部件。考虑到这一点,有两种可能的解决方案。

    选项 1

    您可以将一个类添加到 side.php 文件中的 ul 并从您的样式表中定位它(如果需要)。

    aside.php

    <ul class="archive-sidebar">
        <?php wp_get_archives( array( 'type' => 'monthly') ); ?>
    </ul>
    

    style.css

    archive-sidebar a {
        // Some funky styles
    }
    

    选项 2

    如果上述内容不是您所追求的,您可以在 wp 调用中添加一个过滤器。此代码未经测试,但至少应该为您指明正确的方向。将以下内容添加到您的 functions.php 文件中。

    functions.php

    function get_archives_link_mod ( $link_html ) {
        return str_replace("<a", '<a class="archive_link"', $link_html);
    }
    add_filter("get_archives_link", "get_archives_link_mod");
    

    如果您愿意,您可以使用这两个选项来定位您的 javascript 文件中的链接。如果您使用的是 jQuery,您可以使用...

    $('.archive-link a')
    

    $('.archive-link')
    

    取决于您选择的选项。

    【讨论】:

    • 感谢tonyedwardspz,我已经用小部件的functions.php代码更新了问题(我在wp管理面板中激活了这个)。当我使用选项 1 和/或选项 2 代码时,我重新加载浏览器,并且 &lt;ul&lt;a 不显示类属性(显然浏览器代码检查器中生成的 html 的任何内容都已更改)。
    • @jgc412 您能否将整个side.php 代码添加到问题中,以及将其包含在页面/帖子模板中的部分。
    • 完成!,我刚刚更新了问题@tonyedwardspz
    • @jgc412 你的主题中有一个名为sidebar.php 的文件吗?
    • 是的,我有,@tonyedwardspz,我刚刚将此代码添加到问题中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多