为未连接的用户添加回显到我的wordpress侧边栏


Add echo to my wordpress sidebar for non-connected users

我有一个不错的登录小工具,它没有任何链接到非会员(简单访问者)的注册页面。我想在侧边栏上看到Inscription链接,就在登录小部件下面,但当有人登录时就不会了!

这个想法是,我将用echo可视化链接,通过它的css,我可以将其定位为绝对的。

我对php一无所知,我试图在sidebar.php文件中添加一些代码,但它不起作用:

sidebar.php是经典的twentyeleven主题:

<?php
if ( 'content' != $current_layout ) :
?>
        <div id="secondary" class="widget-area" role="complementary">
            <?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
                <?php
                    if ( is_user_logged_in() ) {
                        echo '';
                    else
                        echo '<a class="inscrip-link" href="http://www.igorlaszlo.com/test/register/">Inscription</a>';
                ?>
                <aside id="archives" class="widget">
                    <h3 class="widget-title"><?php _e( 'Archives', 'vihegay' ); ?></h3>
                    <ul>
                        <?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
                    </ul>
                </aside>
                <aside id="meta" class="widget">
                    <h3 class="widget-title"><?php _e( 'Meta', 'vihegay' ); ?></h3>
                    <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 endif; ?>

我在以上代码中添加的代码是:

<?php
    if ( is_user_logged_in() ) {
        echo '';
    else
        echo '<a class="inscrip-link" href="http://www.igorlaszlo.com/test/register/">Inscription</a>';
?>

然后我将css添加到链接类inscrip-link:

.inscrip-link {
position:absolute;
top:300px;
left: 30%;
z-index:1;
}

有人能告诉我怎么做吗?(我也接受更好的解决方案!:)

您的代码似乎缺少几个花括号。

<?php
    if ( !is_user_logged_in() ) 
    {
        echo '<a class="inscrip-link" href="http://www.igorlaszlo.com/test/register/">Inscription</a>';
    }
?>

我更改了你的代码。只有当用户未登录时,才会显示寄存器链接。

最后,我为我的案例找到了一个更好的解决方案,可以将我的相对位置链接添加到侧边栏。。。如果有人有同样的问题:

  • 我在登录小部件下面添加了一个带有铭文链接的文本小部件(#text-7)
  • 在sidebar.php中,我添加了一个具有相同id的小部件(#text-7),当用户登录时,它会为主体提供一个classe(请参阅下面的php代码)
  • 在style.css中,我将样式赋予#text-7小部件,并在body类"登录"时显示:none(请参阅下面的代码)

PHP

        <aside <?php echo (is_user_logged_in() ? 'class="widget widget_text logged-in"' : ''); ?> id="text-7" class="widget">
            <h3 class="widget-title"></h3>
            <div class="center"><a href="http://www.igorlaszlo.com/test/register/">Inscription</a></div>
        </aside>  

CSS

#text-7 {position:relative;margin: -50px auto 30px;z-index:1;}
.logged-in #text-7 {display: none;}

除了我的个人类,我还向小部件添加了小部件应该有的所有其他类(小部件和小部件文本),因为如果没有,它就不适用于我……也许它只是一个"手工"代码,但它工作得很好!