简体   繁体   English

Function get_permalink() 在 if 语句 (Wordpress) 中无法正常工作

[英]Function get_permalink() not working properly in if statement (Wordpress)

I am working on changing how posts in slider on this website ( https://otwartakultura.org/ ) are linked.我正在努力更改本网站 ( https://otwartakultura.org/ ) 上 slider 中的帖子的链接方式。 Now they can link only to post, but I would like to have possibility to link to other pages.现在他们只能链接到帖子,但我希望有可能链接到其他页面。

I have created a new field in ACF ('link').我在 ACF ('link') 中创建了一个新字段。 I wrote if statement - when the 'link' field is not empty, use 'link' value;我写了 if 语句 - 当“链接”字段不为空时,使用“链接”值; else: use post permalink.否则:使用帖子永久链接。 It works for 'link" value, but for "else" it shows homepage url not post url.它适用于“链接”值,但对于“其他”,它显示主页 url 而不是发布 url。

Current working code:当前工作代码:

 <article<?php
    if (has_post_thumbnail()) {
        ?> style="background-image: url('<?php echo  the_post_thumbnail_url('full'); ?>');"<?php
    } ?>>
<a href="<?php echo get_permalink() ?>">
<header>
<div class="title-box">
        <h1><?php echo get_the_title(); ?></h1>
    <span class="read-more">czytaj więcej ></span>
    </div></header></a>
</article> 

Code I have tried to use:我尝试使用的代码:

<article<?php
      if (has_post_thumbnail()) {
    ?> style="background-image: url('<?php echo the_post_thumbnail_url('full'); ?>');"<?php
    } ?>>

<a href="<?php
if (get_field( 'link' ) !==0) {
echo get_field( 'link' ); }
else {
echo get_the_permalink();
} ?>">

<header>
<div class="title-box">
        <h1><?php echo get_title(); ?></h1>
    <span class="read-more">czytaj więcej ></span>
    </div></header></a>
</article>

Please help:)请帮忙:)

Above this:在此之上:

<?php
    while ( have_posts() ) {
        the_post(); ?>
    <article <?php
        if (has_post_thumbnail()) {
            ?> style="background-image: url('<?php echo the_post_thumbnail_url('full'); ?>');"<?php
        } ?>>
    <a href="<?php echo get_permalink() ?>">
    <header>
    <div class="title-box">
            <h1><?php echo get_the_title(); ?></h1>
        <span class="read-more">czytaj więcej ></span>
        </div></header></a>
    </article>
        <?php
    } ?>

Paste this:粘贴这个:

<article style="background-image: url('ADD_SOME_URL');">
    <a href="google.com">
    <header>
    <div class="title-box">
            <h1>Some title</h1>
        <span class="read-more">czytaj więcej ></span>
        </div></header></a>
        </article>

So all Together will look like this:所以 All Together 将如下所示:

<?php
if ( have_posts() ) {
    ?><div id="promobox"><div id="news" class="cycle-slideshow"
        data-cycle-slides="> article"
        data-cycle-pager-template="<span></span>">
    <div class="cycle-pager"></div>


    <article style="background-image: url('ADD_SOME_URL');">
    <a href="google.com">
    <header>
    <div class="title-box">
            <h1>Some title</h1>
        <span class="read-more">czytaj więcej ></span>
        </div></header></a>
        </article>


    <?php
    while ( have_posts() ) {
        the_post(); ?>
    <article <?php
        if (has_post_thumbnail()) {
            ?> style="background-image: url('<?php echo the_post_thumbnail_url('full'); ?>');"<?php
        } ?>>
    <a href="<?php echo get_permalink() ?>">
    <header>
    <div class="title-box">
            <h1><?php echo get_the_title(); ?></h1>
        <span class="read-more">czytaj więcej ></span>
        </div></header></a>
    </article>
        <?php
    } ?>
    </div>
    <a id="archive" href="/archiwum/">archiwum aktualności</a>
    </div>
<?php
}
?>

Comment answer if some thing is not ok and edit hard coded article set background-image: url() and title in <h1> tag.如果某些事情不正常,请评论答案并编辑硬编码background-image: url()<h1>标记中的标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM